import { useConfirm,useCopyText } from '@/hooks/index.js'

// 共享参数
const searchTable = ref(null) 
const issueSearchTable = ref(null)

export const useTableData = () => {
  return {
    searchTable,
    issueSearchTable,
  }
}

export const useCurd = () => {
  const router = useRouter()
  
  // 表格数据
  const tableLoading = ref(false)
  const total = ref(0)
  const tableData = ref([{
    name:'张三',
    img:'https://img.yzcdn.cn/vant/cat.jpeg',
    id:'1',
  }])
  const isIssue = ref(false)

  // 搜索
  const handlerSearch = (data) => {
    console.log('data', data)
  }

  //优惠劵状态列表
  const couponStatusList = [
    {
      label: "进行中",
      value: "1",
    },
    {
      label: "已终止",
      value: "2",
    },
    {
      label: "已发完",
      value: "3",
    },
    {
      label: "已失效",
      value: "4",
    }
  ]

  // 表格配置
  const btnLefts = ref([
    {
      label:'新增优惠劵',
      type:'primary',
      show:()=>true,
      img:new URL("@/assets/icon/btn-add.png",import.meta.url),
      click:(row)=>{
        console.log('add')
        router.push({name:'CouponManagementAdd'})
      }
    },
    {
      label:'发放管理',
      type:'warning',
      show:()=>true,
      click:(row)=>{
        console.log('add')
        isIssue.value = true
      }
    }
  ])
  const btnRights = ref([])
  const tableForms = ref([
    {
      type: "input",
      prop: 'name',
      placeholder: "优惠劵名称",
      label: "",
      model: '',
    },
    {
      type: "select",
      placeholder: "优惠劵状态",
      label: '',
      filterable:true,
      prop: 'source1',
      data:couponStatusList,
      model: "",
    },
    {
      type: "datePicker",
      prop: ["createStartTime", "createEndTime"],
      startPlaceholder: "创建开始时间",
      endPlaceholder: "创建结束时间",
      label: "",
      model: [],
    },
  ])
  const tableConfig = ref([
    {
      label:'优惠劵名称',
      prop:'name',
      width:260,
      textOverflow:true,
    },
    {
      label:'备注名称',
      prop:'name',
      width:100
    },
    {
      label:'优惠金额',
      prop:'name',
      width:100
    },
    {
      label:'使用门槛',
      prop:'name',
      width:120
    },
    {
      label:'使用时间',
      prop:'name',
      width:330
    },
    {
      label:'状态',
      prop:'name',
      width:100
    },
    {
      label:'核销数量/发放数量',
      prop:'name',
      width:160,
      format:({row})=>{
        return `${2}/${2}`
      }
    },
    {
      label:'发放总量',
      prop:'name',
      width:100
    },
    {
      label:'创建时间',
      prop:'name',
      width:120
    },
    {
      type: "action",
      label:'操作',
      width: 250,
      btns:[
        {
          label:()=>'复制',
          type:'primary',
          show:(row)=>true,
          click:(row)=>{
            useCopyText(row.name)
          }
        },
        {
          label:()=>'编辑',
          type:'success',
          show:(row)=>true,
          click:(row)=>{
            console.log('编辑',row)
            router.push({name:'CouponManagementEdit',query:{id:row.id}})
          }
        },
        {
          label:()=>'终止',
          type:'danger',
          show:(row)=>true,
          click:(row)=>{
            ElMessageBox.confirm('确定要终止吗？', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning',
              beforeClose: async (action, instance, done) => {
                const func = async (test) => {
                  return await new Promise((resolve) => {
                    setTimeout(() => {
                      resolve(test)
                    }, 1000)
                  })
                }
                const confirm = await useConfirm(action, instance, done,func,111)
                if(confirm){
                  ElMessage.success('终止成功')
                  searchTable.value.handlerSearch()
                }
              }
            })
          }
        },
      ]
    }
  ])

  return {
    tableLoading,
    total,
    tableData,
    btnLefts,
    btnRights,
    tableConfig,
    tableForms,
    handlerSearch,
    searchTable,
    isIssue
  }
}

export const useIssue = () => {
  const router = useRouter()
  // 表格数据
  const tableLoading = ref(false)
  const total = ref(0)
  const tableData = ref([{
    name:'张三',
    img:'https://img.yzcdn.cn/vant/cat.jpeg',
    id:'1',
  }])
  // 搜素
  const handlerSearch = (data) => {
    console.log('data', data)
  }
  // 发放状态
  const issueStatusList = [
    {
      label: "未开始",
      value: "1",
    },
    {
      label: "发放中",
      value: "2",
    },
    {
      label: "已结束",
      value: "3",
    },
  ]
  // 表格配置
  const btnLefts = ref([
    {
      label:'新增发放',
      type:'primary',
      show:()=>true,
      img:new URL("@/assets/icon/btn-add.png",import.meta.url),
      click:(row)=>{
        console.log('add')
        router.push({name:'IssuesManagementAdd'})
      }
    },
  ])
  const btnRights = ref([])
  const tableForms = ref([
    {
      type: "input",
      prop: 'name',
      placeholder: "发放名称",
      label: "",
      model: '',
    },
    {
      type: "select",
      placeholder: "发放状态",
      label: '',
      filterable:true,
      prop: 'source1',
      data:issueStatusList,
      model: "",
    },
    {
      type: "datePicker",
      prop: ["createStartTime", "createEndTime"],
      startPlaceholder: "创建开始时间",
      endPlaceholder: "创建结束时间",
      label: "",
      model: [],
    },
  ])
  const tableConfig = ref([
    {
      label:'发放名称',
      prop:'name',
      width:260,
      textOverflow:true,
    },
    {
      label:'备注',
      prop:'name',
      width:100
    },
    {
      label:'发放类型',
      prop:'name',
      width:100
    },
    {
      label:'发放对象',
      prop:'name',
      width:120
    },
    {
      label:'发放内容',
      prop:'name',
      width:240,
      textOverflow:true,
    },
    {
      label:'状态',
      prop:'name',
      width:100
    },
    {
      label:'发放时间',
      prop:'name',
      width:330,
    },
    {
      label:'发放结果',
      prop:'name',
      width:120
    },
    {
      label:'创建时间',
      prop:'name',
      width:180
    },
    {
      type: "action",
      label:'操作',
      width: 250,
      btns:[
        {
          label:()=>'复制',
          type:'primary',
          show:(row)=>true,
          click:(row)=>{
            useCopyText(row.name)
          }
        },
        {
          label:()=>'删除',
          type:'danger',
          show:(row)=>true,
          click:(row)=>{
            ElMessageBox.confirm('确定删除该发放吗？', '提示', {
              type: 'warning',
              beforeClose: async (action, instance, done) => {
                const func = async (test) => {
                  return await new Promise((resolve) => {
                    setTimeout(() => {
                      resolve(test)
                    }, 1000)
                  })
                }
                const confirm = await useConfirm(action, instance, done,func,111)
                if(confirm){
                  ElMessage.success('删除成功')
                  issueSearchTable.value.handlerSearch()
                }
              }
            })
          }
        },
      ]
    }
  ])
  return {
    issueTableLoading:tableLoading,
    issueTotal:total,
    issueTableData:tableData,
    issueBtnLefts:btnLefts,
    issueBtnRights:btnRights,
    issueTableConfig:tableConfig,
    issueTableForms:tableForms,
    issueHandlerSearch:handlerSearch,
    issueSearchTable,
  }
}